package com.ptszyxx.popose.module.main.swipe;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ptszyxx.popose.databinding.FragmentSwipeBinding;
import com.ptszyxx.popose.module.main.swipe.adapter.SwipeAdapter;
import com.ptszyxx.popose.module.main.swipe.vm.SwipeVM;
import com.ysg.base.BaseFragment;
import com.ysg.http.data.AppViewModelFactory;
import com.ysg.http.data.entity.home.HomeResult;
import com.ysg.widget.image.fling.SwipeFlingAdapterView;
import java.util.List;

public class SwipeFragment extends BaseFragment<FragmentSwipeBinding, SwipeVM> {
    private SwipeAdapter adapter;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_swipe;
    }

    @Override
    public int initVariableId() {
        return 0;
    }

    @Override
    public SwipeVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(SwipeVM.class);
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.requestList(true);
    }

    @Override
    public void initAdapter() {
        super.initAdapter();
        adapter = new SwipeAdapter();
        binding.swipeView.setAdapter(adapter);

        initListener();
    }

    private void initListener() {
        binding.swipeView.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
            @Override
            public void removeFirstObjectInAdapter() {
                adapter.remove(0);
            }

            @Override
            public void onLeftCardExit(Object dataObject) {
            }

            @Override
            public void onRightCardExit(Object dataObject) {
            }

            @Override
            public void onAdapterAboutToEmpty(int itemsInAdapter) {
                if (itemsInAdapter == 3) {
                    viewModel.requestList(false);
                }
            }

            @Override
            public void onScroll(float scrollProgressPercent) {

            }
        });

        adapter.setOnItemClickListener(new SwipeAdapter.OnClickListener() {
            @Override
            public void onCloseClick(HomeResult item) {
                binding.swipeView.getTopCardListener().selectLeft();
            }

            @Override
            public void onMessageClick(HomeResult item) {
                YActivityUtil.getInstance().jumpUserDetail(viewModel, item.getUserid());
            }

            @Override
            public void onLikeClick(HomeResult item) {
                binding.swipeView.getTopCardListener().selectRight();
            }
        });
    }


    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.onRefreshEvent.observe(this, new Observer<List<HomeResult>>() {
            @Override
            public void onChanged(List<HomeResult> o) {
                adapter.addAll(o);
            }
        });
    }
}
